<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%> 
<html>
<style type="text/css">
pre {
	white-space: pre-wrap;
	word-wrap: break-word;
}
</style>
<head>
		<meta charset="utf-8" />
		<meta name="keywords" content="pinpin社保" />
		<meta name="description" content="pinpin社保" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
        <meta name="renderer" content="webkit" />
        <meta name="viewport" content="width=device-width, initial-scale=1">
		<title>社保运营后台-政务内容</title>
		<link rel="stylesheet" href="/as/css/bootstrap.css" />
		<link rel="stylesheet" href="/as/css/layout.css" />
	<%@ include file="/WEB-INF/jsp/common/common.jsp" %>
</head>
<body>
	<div id="wrapper">
		<%@ include file="/WEB-INF/jsp/common/menu.jsp" %>
        <!-- Page Content -->
        <div id="page-wrapper">
        	<div class="row">
                <div class="col-lg-12">
                    <!-- 路径导航 -->
                    <ol class="breadcrumb">
				      <li>批量增加内容</li>
				      <li><a href="${list_uri }">政务内容列表</a></li>
				    </ol>
                </div>
            </div>
            <div class="row">
                <div class="col-lg-12">
                    <div class="col-md-12">
 			    		<textarea name="content" id="content" rows="15" cols="" onblur="displayContent()" class="form-control" placeholder="请贴进带有标签格式的内容"></textarea> 
                    </div>
                </div>                
                <div class="col-lg-12" style="margin-top:20px">
                	<div class="col-lg-2"><span style="font-size:30px">类型：</span><span id="type"></span></div>
                	<div class="col-lg-2"><span style="font-size:30px">城市：</span><span id="city"></span></div>
                	<div class="col-lg-7"><span style="font-size:30px">标题：</span><span id="title"></span></div> 
                	<div class="col-lg-1">
                		<input type="button" id="btn" onclick="settingContent()"  value="保存" class="btn btn-outline btn-primary" >
                	</div>  
                </div>      
                <div class="col-lg-12">    
                	<div class="col-lg-2"></div>
                	<div id="subs" class="col-lg-8">
                	
                	</div>
                	<div class="col-lg-2"></div>
                </div>         
            </div>
        </div>
        <!-- /#page-wrapper -->
    </div>
    <!-- /#wrapper -->
</body>
<script type="text/javascript">
var setting_uri = "${setting_uri}";

var c, typeV, cityV;
var titleArr=new Array(), cStrArr=new Array(); // 标签数组，小标题和内容的字符串数组。
var contentArr=new Array();  // 数组里包含标题对应的内容数组。
var subArr = new Array();
var resultArr = new Array();

function init() {
	titleArr = [];
	cStrArr = [];
	contentArr = [];
	subArr = [];
	resultArr=[];
	$("#subs").html("");
}

function displayContent() {
	init();
	c = $("#content").val().trim();
	
	typeV = dis("<h1>", "</h1>");
	$("#type").text(typeV);
	
	cityV = dis("<h1>", "</h1>");
	$("#city").text(cityV);

	dealTitleContent();
	$("#title").text(titleArr);
	dealSubs();
	console.log("标题：", titleArr);
	console.log("内容：", contentArr);	
	pageView();
}

function dealTitleContent() {
	// 拆分标题与内容到各自的数组
	for (var k=0; k<10; k++) {
		var hindex = c.indexOf("<h2>");
		if (hindex > -1) {
			var hindex2 = c.indexOf("</h2>");
			titleV = c.substring(hindex+4, hindex2);
			console.log("标题：", k, titleV);
			titleArr[k] = titleV;
			
			c = c.substring(hindex2 + 5); // 截去了上面的标题
			hindex = c.indexOf("<h2>");
			if (hindex == -1) {
				// 余下全是内容
				cStrArr[k] = c;
			} else {
				cStrArr[k] = c.substring(0, hindex); // 上面标题对应的内容
			}
		} else {
			break;
		}
	}
}
function dealSubs() {
	for (var i=0; i<cStrArr.length; i++) {
		c = cStrArr[i];
		subArr = [];
		
		for (var cindex=0; cindex< 100; cindex++) {
			var subTitle = dis("<h3>", "</h3>");
			if (subTitle == "") {
				break;
			}
			
			var subContent = dis("<div>", "</div>");
			if (subContent.indexOf("<p>") > -1) {
				//subContent = subContent.replace(/<([\/]?)(p)((:?\s*)(:?[^>]*)(:?\s*))>/g ,"\r\n\r\n");
				subContent = subContent.replace(/<p>/g ,"\r\n");
				subContent = subContent.replace(/<\/p>/g ,"\r\n");
			}
			subArr[cindex] = {t: subTitle, c: subContent};
		}
		contentArr[i] = subArr;
	}	
}

function dis(tag, endTag) {
	//console.log("c=", c);
	var index = c.indexOf(endTag);
	if (index == -1) {
		return "";
	}
	var v = c.substring(0, index);
	v = v.replace(tag, "");
	c = c.substring(index + endTag.length);
	return v;
}

function pageView() {
	var subs = $("#subs");
	for (var t=0; t<titleArr.length; t++) {
		subs.append("<h2 style='color:red'>" + titleArr[t] + "</h2>");
		var tcArr = contentArr[t];
		for (var cc=0; cc<tcArr.length; cc++) {
			var tc = tcArr[cc];
			subs.append("<h3 style='text-align:center'>" + tc.t + "</h3>");
			subs.append("<pre>" + tc.c + "</pre>");
		}
	}
}

function settingContent() {
	if (titleArr.length == 0 || titleArr.length != contentArr.length) {
		notice("未发现有内容需要处理。", 5000);
		return;
	}
	$("#btn").attr("disabled", true);
	
	for (var t=0; t<titleArr.length; t++) {
		var data = {
				type: typeV,
				city: cityV,
				title: titleArr[t],
				subs: JSON.stringify(contentArr[t])
		}
		$.post(setting_uri, data, function(ret,status){
			smokeAlert(ret.code, ret.msg);
		});
	}
	$("#btn").removeAttr("disabled")
}
</script>
</html>